package com.kool.leitu.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.leitu.bean.io.CodeRequest;
import com.kool.leitu.bean.io.CodeResponse;
import com.kool.leitu.service.CodeService;
import com.kool.user.bean.io.LoginRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: tianhe
 * @create: 2019/02/11 17:16
 */
@RestController
@Slf4j
public class CodeController {
    @Autowired
    private CodeService codeService;

    /**
     * 生成代码
     *
     * @param input
     * @return
     */
    @PostMapping("/dev/code/gen")
    public JSONObject genCode(@RequestBody String input) {
        JSONObject joIn = JSON.parseObject(input);
        JSONArray jaSecIn = joIn.getJSONArray("a");
        JSONObject joPropIn = jaSecIn.getJSONObject(0);
        CodeRequest request = JSON.toJavaObject(joPropIn, CodeRequest.class);
        CodeResponse response = codeService.genCode(request.getPkg(), request.getTables());

        JSONObject output = new JSONObject();
        JSONArray jaSec = new JSONArray();
        jaSec.add(response);
        output.put("a", jaSec);
        return output;
    }
}
